package com.situ.mall.controller.back;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.PageView;
import com.situ.mall.entity.Result;
import com.situ.mall.service.IUserService;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * 记录上次访问网站的时间
 * Created by pc on 17-3-30.
 */
@Controller
@RequestMapping("/CookieServlet")
public class CookieServlet {
	@Autowired
	private IUserService userService;
	
	@RequestMapping("/cookie.action")
	@ResponseBody
    public Result cookie(HttpServletRequest request, HttpServletResponse response,Model model) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		String lastTime=null;
		String last = null;
        Cookie [] cookies = request.getCookies();
        for(int i=0 ;cookies!=null&&i<cookies.length;i++){
            if("time1".equals(cookies[i].getName())){
                lastTime=cookies[i].getValue();
                break;
            }
        }
        if(lastTime==null){
        	last = "您今天第一次访问该网站";
        }else {
        	last = "您上次访问时间为："+lastTime+"";
        }
        String time = new SimpleDateFormat("YYYY-MM-dd-hh:mm:ss").format(new Date());
        Cookie cookie=new Cookie("time1",time);
        response.addCookie(cookie);
        return Result.success(last);
    }
	

}